﻿using DevExpress.XtraBars.Customization;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinForm_Mes.ProductManagement;
using WinForm_Mes.CheckManagement;
using WinForm_Mes.Process;
using WinForm_Mes.WarehouseManagement;
using WinForm_Mes.WorkingManagement.TeamType;
using WinForm_Mes.WorkingManagement.TeamSet;

namespace WinForm_Mes
{
    public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public Form1()
        {
            InitializeComponent();
            
            // 绑定客户管理菜单点击事件
            this.navBarItem33.LinkClicked += UserShowClick;
            
            // 绑定报价单-型号管理菜单点击事件
            this.navBarItem35.LinkClicked += VersionShowClick;
        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

        }

        private void navBarControl1_Click(object sender, EventArgs e)
        {




        }

        private void PurchaseClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                PurchaseOrderView purchaseOrderView = new PurchaseOrderView();
                purchaseOrderView.MdiParent = this;
                purchaseOrderView.Show();
            }
        }
        /// <summary>
        /// 基础设置—计量单位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BaseSetClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                BaseSet baseset = new BaseSet();
                baseset.MdiParent = this;
                baseset.Show();
            }
        }


        private void ProcessStepUpClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                ProcessStepUp processStepUp = new ProcessStepUp();
                processStepUp.MdiParent = this;


                HttpClient client = new HttpClient();



                client.BaseAddress = new Uri("http://localhost:5053/");


                var response = client.GetAsync("api/ProcessManagement/GetProcessStepUpList").Result;

                if (response.IsSuccessStatusCode)
                {
                    var context = response.Content.ReadAsStringAsync().Result;

                    processStepUp.LoadData(context);
                }
                else
                {
                    MessageBox.Show($"获取数据失败: {response.StatusCode}", "错误",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }



                processStepUp.Show();
            }
        }


        private void CheckWayClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                CheckWay checkway = new CheckWay();
                checkway.MdiParent = this;
                checkway.Show();
            }
        }

        private void CheckItemClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                CheckItem checkitem = new CheckItem();
                checkitem.MdiParent = this;
                checkitem.Show();
            }
        }
        /// <summary>
        /// 产品管理—产品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Product(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                ProductForm product = new ProductForm();
                product.MdiParent = this;
                product.Show();
            }
        }

        private void navBarItem31_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                CheckStandardForm product = new CheckStandardForm();
                product.MdiParent = this;
                product.Show();
            }
        }
        /// <summary>
        /// 产品管理—型号管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductType(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                ProductTypeForm type = new ProductTypeForm();
                type.MdiParent = this;
                type.Show();
            }
        }

        private void navBarItem32_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                CheckManagement.检验单据 document = new CheckManagement.检验单据();
                document.MdiParent = this;
                document.Show();
            }
        }

        private void WarehouseClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                WarehouseForm warehouseForm = new WarehouseForm();
                warehouseForm.MdiParent = this;
                warehouseForm.Show();
            }
           
        }
        /// <summary>
        /// 产品管理—规格管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductSpecification(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                ProductSpecificationForm specification = new ProductSpecificationForm();
                specification.MdiParent = this;
                specification.Show();
            }
        }

        //private void navBarItem28_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        //{
        //    bool result = false;
        //    //遍历所有子窗体，找到已经打开的子窗体，并显示拒绝重复打开
        //    foreach (var item in this.MdiChildren)
        //    {
        //        if (item.Text == e.Link.Caption)
        //        {
        //            item.Activate();
        //            result = true;
        //            break;
        //        }
        //    }
        //    if (!result)
        //    {
        //        SupperForm document = new SupperForm();
        //        document.MdiParent = this;
        //        document.Show();
        //    }
        //}


        private void TeamTypeClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                TeamTypeForm teamType = new TeamTypeForm();
                teamType.MdiParent = this;
                teamType.Show();
            }
        }

        private void TechnologicalClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                TechnologicalFlow technological = new TechnologicalFlow();
                technological.MdiParent = this;
                technological.Show();
            }
        }

        private void TeamSetClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            var result = false;
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }

            if (!result)
            {
                TeamSetForm teamSet = new TeamSetForm();
                teamSet.MdiParent = this;
                teamSet.Show();
            }
        }

        private void navBarItem28_LinkClicked_1(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            var result = false;
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }

            if (!result)
            {
                SupperForm supper = new SupperForm();
                supper.MdiParent = this;
                supper.Show();
            }
        }

        private void navBarItem51_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            var result = false;
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }

            if (!result)
            {
                WarehouseAreaForm warehouseAreaForm = new WarehouseAreaForm();
                warehouseAreaForm.MdiParent = this;
                warehouseAreaForm.Show();
            }

        }

        private void navBarItem52_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            var result = false;
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == e.Link.Caption)
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }

            if (!result)
            {
                WarehouseAddressForm warehouseAddressForm = new WarehouseAddressForm();
                warehouseAddressForm.MdiParent = this;
                warehouseAddressForm.Show();
            }
        }

        /// <summary>
        /// 客户管理菜单点击事件
        /// </summary>
        private void UserShowClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            // 遍历所有子窗体，找到已经打开的客户管理窗体，拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == "客户管理")
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                UserShowForm userShowForm = new UserShowForm();
                userShowForm.MdiParent = this;
                userShowForm.Show();
            }
        }
        
        /// <summary>
        /// 报价单-型号管理菜单点击事件
        /// </summary>
        private void VersionShowClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            // 遍历所有子窗体，找到已经打开的报价单-型号管理窗体，拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == "报价单-型号管理")
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                VersionShowForm versionShowForm = new VersionShowForm();
                versionShowForm.MdiParent = this;
                versionShowForm.Show();
            }
        }

        private void navBarItem35_ItemClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            VersionShowClick(sender, e);
        }

        private void navBarItem36_ItemClick(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            QuotationShowClick();
        }

        private void QuotationShowClick()
        {
            CheckManagement.QuotationShowForm quotationShowForm = new CheckManagement.QuotationShowForm();
            quotationShowForm.MdiParent = this;
            quotationShowForm.Show();
        }

        private void navBarItem30_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            // 遍历所有子窗体，找到已经打开的报价单-型号管理窗体，拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == "数据大屏")
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                LargeScreenDisplayForm largeScreenDisplayForm = new LargeScreenDisplayForm();
                largeScreenDisplayForm.MdiParent = this;
                largeScreenDisplayForm.Show();
            }
        }

        private void navBarItem7_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool result = false;
            // 遍历所有子窗体，找到已经打开的报价单-型号管理窗体，拒绝重复打开
            foreach (var item in this.MdiChildren)
            {
                if (item.Text == "设备类型")
                {
                    item.Activate();
                    result = true;
                    break;
                }
            }
            if (!result)
            {
                EquipmentTypeForm equipmentTypeForm = new EquipmentTypeForm();
                equipmentTypeForm.MdiParent = this;
                equipmentTypeForm.Show();
            }
        }
    }

}

public class Item
{
    public long Id { get; set; }
    /// <summary>
    /// 工序编码
    /// </summary>
    public string ProcessCode { get; set; }
    /// <summary>
    /// 工序名称
    /// </summary>
    public string ProcessName { get; set; }
    /// <summary>
    /// 是否启用
    /// </summary>
    public bool IsEnabled { get; set; }
    /// <summary>
    /// 准备时间(分钟)
    /// </summary>
    public decimal PreparationTime { get; set; }
    /// <summary>
    /// 生产时间(分钟)
    /// </summary>
    public decimal ProductionTime { get; set; }
    /// <summary>
    /// 工序说明
    /// </summary>
    public string ProcessDescription { get; set; }
    /// <summary>
    /// 是否质检
    /// </summary>
    public bool IsQualityCheck { get; set; }
    /// <summary>
    /// 备注
    /// </summary>
    public string Remark { get; set; }
}



public class ProcessStepUpList
{
    public int total { get; set; }
    public int pageIndex { get; set; }
    public int pageSize { get; set; }
    public Item[] items { get; set; }
    public int code { get; set; }
    public string message { get; set; }
    public object data { get; set; }
}